// @Author EthanScriptOn
// @Desc
package config_freighter

import (
	"context"
	"errors"
	"gitee.com/fatzeng/srf_switch_basic_components/processor"
	"gitee.com/fatzeng/srf_switch_config/common"
	"gitee.com/fatzeng/srf_switch_config/config_context"
	"gitee.com/fatzeng/srf_switch_config/config_parser"
	"gitee.com/fatzeng/srf_switch_protocol/protocol/log"
)

type FundamentalFreightProcessorConfigurator struct {
	processor.Processor
	config_parser.ResourceFreighter
	common.FundamentalComponent
}

func (f *FundamentalFreightProcessorConfigurator) Initialization() (err error) {
	log.Logger().Info("You can implement this function to perform some preprocessing operations")
	return
}

func (f *FundamentalFreightProcessorConfigurator) Process(ctx context.Context, pendingProcessingData interface{}) (err error) {
	configInitializationContext, ok := ctx.(*config_context.ConfigInitializationContext)
	if !ok {
		return errors.New("need *switchContext.ConfigInitializationContext type config_context")
	}
	if err = f.ExecuteFreight(configInitializationContext, pendingProcessingData); err != nil {
		return
	}
	return
}

func (f *FundamentalFreightProcessorConfigurator) ExecuteFreight(ctx *config_context.ConfigInitializationContext, completedData interface{}) error {
	currentReferenceProxy, err := f.GetCurrentReferenceProxy(ctx)
	if err != nil {
		return err
	}
	if err = f.HandleCallErr(currentReferenceProxy.Call(config_parser.ResourceFreighter.FreightPrepare, ctx)); err != nil {
		return err
	}
	if err = f.HandleCallErr(currentReferenceProxy.Call(config_parser.ResourceFreighter.Freight, ctx, completedData)); err != nil {
		return err
	}
	return f.HandleCallErr(currentReferenceProxy.Call(config_parser.ResourceFreighter.FreightDoMore, ctx))
}
